//
//  main.swift
//  score
//
//  Created by gaoyi on 2017/9/14.
//  Copyright © 2017年 gaoyi. All rights reserved.
//

import Foundation
struct Student{
    var name:String
    var mathScore:Int
    var englishScore:Int
    var average:Float
    init(name:String,mathScore:Int,englishScore:Int) {
        self.name = name
        self.mathScore = mathScore
        self.englishScore = englishScore
        self.average = Float(self.mathScore + self.englishScore) / 2
    }
}
func sortWithAverage(students:[Student])->[Student]{
    var result=students;
    for i in 0...9 {
        for j in i...9{
            if result[i].average<result[j].average{
                let temp=result[i]
                result[i]=result[j]
                result[j]=temp
            }
        }
    }
    return result
}
//构造10个学生对象
var student01 = Student(name: "st01", mathScore: 66, englishScore: 77)
var student02 = Student(name: "st02", mathScore: 88, englishScore: 12)
var student03 = Student(name: "st03", mathScore: 35, englishScore: 99)
var student04 = Student(name: "st04", mathScore: 66, englishScore: 15)
var student05 = Student(name: "st05", mathScore: 88, englishScore: 97)
var student06 = Student(name: "st06", mathScore: 44, englishScore: 94)
var student07 = Student(name: "st07", mathScore: 97, englishScore: 25)
var student08 = Student(name: "st08", mathScore: 77, englishScore: 99)
var student09 = Student(name: "st09", mathScore: 34, englishScore: 78)
var student10 = Student(name: "st10", mathScore: 87, englishScore: 99)

//调用排序方法
var result = sortWithAverage(students: [student01,
                                        student02,
                                        student03,
                                        student04,
                                        student05,
                                        student06,
                                        student07,
                                        student08,
                                        student09,
                                        student10])

print("英语和数学平均分的成绩从小到大排列为:")
//遍历结果数组并打印
for i in 0..<result.count{
    print("姓名:\(result[i].name),数学成绩:\(result[i].mathScore),英语成绩:\(result[i].englishScore),平均分:\(result[i].average)")
}
